package com.example.cofeed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import  com.example.cofeed.util.ToastUtils;;
import com.example.cofeed.R;
import com.example.cofeed.adapter.PinZhongAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.entity.Navigator;
import com.example.cofeed.entity.RegisterThird;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PinZhongActivity extends BaseActivity {
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    private PinZhongAdapter mPinZhongAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pinzhong;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {

        setMainTitle("关注品种");

       mPinZhongAdapter = new PinZhongAdapter();
       mRvList.setLayoutManager(new GridLayoutManager(this, 4));
       mRvList.setAdapter(mPinZhongAdapter);
        mPinZhongAdapter.setOnItemClickListener((adapter, view, position) -> {
            Navigator navigator = (Navigator) adapter.getData().get(position);
            mPinZhongAdapter.select(navigator);

        });

        getData();

    }

    private void getData(){

        ApiService.register_catalogs(this, new DialogCallback<LzyResponse<List<Navigator>>>() {
            @Override
            public void onSuccess(Response<LzyResponse<List<Navigator>>> response) {
                mPinZhongAdapter.setNewData(response.body().data);

            }
        });

    }

    @OnClick(R.id.layout_bottom)
    public void onViewClicked() {

        List<Integer> stringList = new ArrayList<>();
        for (Navigator navigator:mPinZhongAdapter.getData()){
            if (navigator.isSelect){
                stringList.add(navigator.catid);
            }
        }
        if (ObjectUtils.isEmpty(stringList)){
            ToastUtils.showShort("至少选一项");
            return;
        }
        ApiService.register_third(this, stringList, new DialogCallback<LzyResponse<RegisterThird>>() {
            @Override
            public void onSuccess(Response<LzyResponse<RegisterThird>> response) {
                Intent intent = new Intent(mContext, RegisterSucessActivity.class);
                intent.putExtra("read_num", response.body().data.read_num);
                startActivity(intent);
                finish();

            }
        });

    }
}
